package org.irri.smta.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "MATERIAL")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Material extends TimestampedAutoIdEntity implements Comparable<Material> {
	
	private static final long serialVersionUID = 1L;

	private static final int CROP_LENGTH = 64;
	private static final int SAMPLE_ID_LENGTH = 128;
	private static final int PGRFA_UNDER_DEVELOPMENT_LENGTH = 1;
	private static final int ANCESTRY_LENGTH = 65536;
	
	@NotNull
	@Column(name = "GID", nullable = false)
	private Long gid;

	@NotNull
	@Column(name = "CROP", nullable = false)
	@Size(min = 1, max = CROP_LENGTH)
	private String crop;

	@NotNull
	@Column(name = "SAMPLE_ID", nullable = false)
	@Size(min = 1, max = SAMPLE_ID_LENGTH)
	private String sampleId;

	@NotNull
	@Column(name = "PUD", nullable = false)
	@Size(min = 1, max = PGRFA_UNDER_DEVELOPMENT_LENGTH)
	private String pud;

	@Lob
	@NotNull
	@Column(name = "ANCESTRY", nullable = false)
	@Size(min = 1, max = ANCESTRY_LENGTH)
	private String ancestry;

	public Material() {
		super();
	}

	public Long getGid() {
		return gid;
	}

	public void setGid(Long gid) {
		this.gid = gid;
	}

	public String getCrop() {
		return crop;
	}

	public void setCrop(String crop) {
		this.crop = crop;
		if (crop != null) {
			this.crop = StringUtils.substring(crop.trim(), 0, CROP_LENGTH);
		}
	}

	public String getSampleId() {
		return sampleId;
	}

	public void setSampleId(String sampleId) {
		this.sampleId = sampleId;
		if (sampleId != null) { 
			this.sampleId = StringUtils.substring(sampleId.trim(), 0, SAMPLE_ID_LENGTH);
		}
	}

	public String getPud() {
		return pud;
	}

	public void setPud(String pud) {
		this.pud = pud;
		if (pud != null) {
			this.pud = StringUtils.substring(pud.trim(), 0, PGRFA_UNDER_DEVELOPMENT_LENGTH);
		}
	}

	public String getAncestry() {
		return ancestry;
	}

	public void setAncestry(String ancestry) {
		this.ancestry = ancestry;
		if (ancestry != null) {
			this.ancestry = StringUtils.substring(ancestry.trim(), 0, ANCESTRY_LENGTH);
		}
	}

	@Override
	public int compareTo(Material o) {
		if (o != null) {
			return this.getId().compareTo(o.getId());
		}
		return 0;
	}

	@Override
	public String toString() {
		return "Material [gid=" + gid + ", crop=" + crop + ", sampleId="
				+ sampleId + ", pud=" + pud + ", ancestry=" + ancestry + "]";
	}

}
